package com.deer.wms.base.system.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.base.system.dao.*;
import com.deer.wms.base.system.model.*;
import com.deer.wms.base.system.model.Area.AreaInfo;
import com.deer.wms.base.system.model.Item.*;
import com.deer.wms.base.system.model.Item.AuxProp;
import com.deer.wms.base.system.model.Supplier.SuppInfoBO;
import com.deer.wms.base.system.model.Supplier.SupplierManage;
import com.deer.wms.base.system.model.erp.*;
import com.deer.wms.base.system.model.http.HttpClientss;
import com.deer.wms.base.system.service.AreaInfoService;
import com.deer.wms.base.system.service.ErpService;
import com.deer.wms.base.system.service.ItemInfoService;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.service.SuperServiceImpl;
import com.deer.wms.project.root.util.CollectionUtils;
import com.deer.wms.project.root.util.HttpClient;
import com.deer.wms.system.manage.dao.DeptMapper;
import com.deer.wms.system.manage.model.customer.Customer;
import com.deer.wms.system.manage.model.dept.Dept;
import com.deer.wms.system.manage.service.CustomerService;
import com.deer.wms.system.manage.service.DeptService;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ErpServiceImpl extends SuperServiceImpl<ErpBackRecordMapper, ErpBackRecord> implements ErpService {

//    @Value("${lzb.erp.ip}")
//    private String erpIp;

    @Autowired
    private BillInfoMapper billInfoMapper;

    @Autowired
    private BillDetailMapper billDetailMapper;

    @Autowired
    private ItemInfoMapper itemInfoMapper;

    @Autowired
    private DeptInfoMapper deptInfoMapper;

    @Autowired
    private CustInfoMapper custInfoMapper;

    @Autowired
    private PurOrderMapper purOrderMapper;

    @Autowired
    private PurOrderDetailMapper purOrderDetailMapper;

    @Autowired
    private AuxPropMapper auxPropMapper;
    @Autowired
    private ItemInfoService itemInfoService;
    @Autowired
    private AreaInfoService areaInfoService;

    @Autowired
    private DeptService deptService;

    @Autowired

    private DeptMapper deptMapper;

    @Autowired
    private SupplierManageServiceImpl supplierManageService;
    @Autowired
    private CustomerService customerService;


    private  String BASE_URL = "http://61.160.125.186:5555";  //正式服务器
    @Override
    public void saveItemInfo() {


        for (int i = 0; i < 10; i++) {
//            ;
            int finalI = i;
//            CompletableFuture.runAsync(()->{
            this.saveItemInfo(finalI, 50000);
//        });
        }



    }

    public void saveItemInfo(int startPage, int end ) {

        try {
            String token = getToken();
            Map<String, Integer> map1 = new HashMap<>();
            map1.put("currentPage", startPage);
            map1.put("pageSize", end);
            try {
                //调用erp接口获取数据
                CloseableHttpClient client = HttpClients.createDefault();
                URIBuilder uriBuilder = new URIBuilder(BASE_URL+"/api/ICItem/GetRangeICItems");
                HttpPost post = new HttpPost(uriBuilder.build());
                StringEntity myEntity = new StringEntity(JSON.toJSONString(map1), "UTF-8");// 构造请求数据
                post.setHeader("Content-Type", "application/json");
                post.addHeader("Authorization", "Bearer " + token);
                post.setEntity(myEntity);// 设置请求体
                String responseContent = null; // 响应内容
                CloseableHttpResponse response = null;
                response = client.execute(post);
                HttpEntity entity = response.getEntity();
                if (response.getStatusLine().getStatusCode() == 200) {
                    String content = EntityUtils.toString(entity, "UTF-8");
                    ItemResponseBO itemResponseBO = JSONArray.parseObject(content, ItemResponseBO.class);
                    String it = itemResponseBO.getList();
                    List<ItemErpBO> itemErpBOS = (List<ItemErpBO>) JSON
                            .parseArray(it, ItemErpBO.class);
                    this.insertItemInfo(itemErpBOS);
                }

            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }

        }

        catch (Exception e) {
            e.printStackTrace();
        }

    }



    @Value("${erp.ClientID}")
    private String clientID;
    @Value("${erp.ClientSecret}")
    private String ClientSecret;
    @Value("${erp.tokenUrl}")
    private String tokenUrl;

    public String getToken() {
//        //请求token
        MultiValueMap<String, String> stringMultiValueMap = new LinkedMultiValueMap<>();

        stringMultiValueMap.add("client_id", this.clientID);
        stringMultiValueMap.add("client_secret", this.ClientSecret);
        stringMultiValueMap.add("grant_type", "password");
        stringMultiValueMap.add("username", "epichust");
        stringMultiValueMap.add("password", "hK@vmz0YI1e^thtB");
        String tokenUrl = this.tokenUrl+"/connect/token";
        System.out.println(tokenUrl);
        System.out.println(this.clientID);
        System.out.println(this.ClientSecret);
        String clientRg = null;
        try {
            clientRg = HttpClient.clientRg(tokenUrl, HttpMethod.POST, stringMultiValueMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONObject.parseObject(clientRg);
        return jsonObject.getString("access_token");
        //过期时间
//        String pasts = jsonObject.getString("expires_in");
    }


    public void insertItemInfo(List<ItemErpBO> list) {
        List<ItemInfo> itemInfos = new ArrayList<>();
        for (ItemErpBO bo : list) {
            ItemInfo itemInfo = new ItemInfo();
            itemInfo.setId(bo.getId());
            itemInfo.setErpItemId(bo.getId());
            itemInfo.setDefaultStockId(bo.getDefaultStockId());
            itemInfo.setDefaultStockName(bo.getDefaultStockName());
            itemInfo.setFeedCategory(bo.getFeedCategory()); //发料属性
            itemInfo.setCategory(bo.getCategory()); //通用属性
            itemInfo.setItemCode(bo.getNumber());//物料编码
            itemInfo.setItemName(bo.getName());//物料名称
            itemInfo.setPackCode("sys_pack");
            itemInfo.setItemStatus(bo.getEnabled() == false ? "disable" : "enable");//启用
            itemInfo.setSpec(bo.getModel());//规格
            itemInfo.setModel(bo.getModel());//型号
            itemInfo.setFloorLimit(bo.getUnitId());//计量单位ID
            itemInfo.setUnitName(bo.getUnitName());//主计量单位名称
            itemInfo.setOrganizationId(-1);//物料ID
            itemInfo.setItemBarCode(bo.getErpClsName());//物料属性
            itemInfo.setItemTypeId(bo.getAuxPropClassId());//物料辅助属性类别ID
//         //批次管理
            itemInfo.setMemo(bo.getBatchManager() == false || bo.getBatchManager() == null ? "0" : "1");
            itemInfo.setWeight(bo.getQuality());//材质
            itemInfo.setBatchTacticCode(bo.getShelfLifeDays().toString());
            itemInfos.add(itemInfo);
        }
        if (ObjectUtil.isNotEmpty(itemInfos)) {
            itemInfoService.saveBatch(itemInfos);
        }
    }




    @Value("${prj.name}")
    private String prjName;

    @Override
    public String saveWarehouseInfo() {
        if (!"jy".equals(prjName)) {
            return "";
        }

        try {
            String token = getToken();
            if( token!= null){
                //调用erp接口获取数据
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.add("Accept", "application/json");
                httpHeaders.add("Authorization", "Bearer " + token);
                String url = BASE_URL+"/api/Stock/GetAllStocks";
                String result = HttpClient.getRequest(url, null, httpHeaders);
                JSONArray jsonArray = JSONArray.parseArray(result);
                List<WarehouseBO> ws = JSONObject.parseArray(jsonArray.toJSONString(), WarehouseBO.class);
                List<AreaInfo> areaInfoList = new ArrayList<>();
                this.saveBatch(ws);

            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public void saveBatch(List<WarehouseBO> list) {
        List<AreaInfo> areaInfoList = new ArrayList<>();
        Set<Integer> collect = list.stream().map(WarehouseBO::getId).collect(Collectors.toSet());
        Set<Integer> areaCode = areaInfoService.list(new QueryWrapper<AreaInfo>().in("erp_area_id", collect)).stream().map(AreaInfo::getErpAreaId).collect(Collectors.toSet());
        for (WarehouseBO item : list) {
            if (areaCode.contains(item.getId())) {
                continue;
            }
            AreaInfo areaInfo = new AreaInfo();
            areaInfo.setAreaCode(item.getNumber());
            areaInfo.setAreaName(item.getName());
            areaInfo.setWareId(285);
            areaInfo.setErpAreaId(item.getId());
            areaInfoList.add(areaInfo);
        }

        areaInfoService.saveBatch(areaInfoList);

    }
    @Override
    public String saveDeptInfo() {
        try {
            String token = getToken();
            if(token != null){
                //调用erp接口获取数据
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.add("Accept", "application/json");
                httpHeaders.add("Authorization", "Bearer " + token);
                String url = BASE_URL+"/api/dept/getalldepts";
                String result = HttpClient.getRequest(url, null, httpHeaders);
                JSONArray jsonArray = JSONArray.parseArray(result);
                List<DeptInfoBO> di = JSONObject.parseArray(jsonArray.toJSONString(), DeptInfoBO.class);
                deptMapper.deleteDept();
                List<Dept> list = new ArrayList<>();
                if(di.size() != 0){
                    for(DeptInfoBO bo : di){
                        Dept dept = new Dept();
                        dept.setDeptCode(bo.getNumber());
                        dept.setDeptName(bo.getName());
                        dept.setDeptId(bo.getId());
                        dept.setEnabled(bo.getEnabled());
                        list.add(dept);
                    }
                    deptService.saveBatch(list);
                }
            }
            return null;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    @Override
    public String saveCustomerInfo() {

        try {
//            String token = getToken();
//            if(token != null){
//                //调用erp接口获取数据
//                HttpHeaders httpHeaders = new HttpHeaders();
//                httpHeaders.add("Accept", "application/json");
//                httpHeaders.add("Authorization", "Bearer " + token);
//                String url = BASE_URL+"/api/Customer/GetAllCustomers";
            String url = BASE_URL + "/zp/api/ZpViewInfo/Query";
            Map<String, String> map = new HashMap<>();
            map.put("wldwType", "1");
            Map<String, Object> returnMap = HttpClientss.getDateFromERP(url, HttpMethod.POST, map);
            if (ObjectUtil.equals(returnMap.get("code"), 200)) {
                if (ObjectUtil.equals(returnMap.get("code"), 200)) {
                    if (ObjectUtil.isNotNull(returnMap.get("data")) && StringUtils.isNotEmpty(returnMap.get("data").toString())) {
                        JSONArray jsonArray = JSONArray.parseArray(String.valueOf(returnMap.get("data")));
                        List<Customer> list = new ArrayList<>();
                        List<SuppInfoBO> ci = JSONObject.parseArray(jsonArray.toJSONString(), SuppInfoBO.class);

                        if (ci.size() != 0) {
                            List<Customer> oldCustomers = customerService.list();
                            Map<String, Customer> oldCustomerMap = CollectionUtils.convertMap(oldCustomers, Customer::getCustomerCode);
                            for (SuppInfoBO bo : ci) {
//                              //先判断抓取的数据在数据库中是否存在
                                if (ObjectUtil.isNull(oldCustomerMap.get(bo.getWldwCode()))) {
                                    Customer customer = new Customer();
                                    customer.setCustomerCode(bo.getWldwCode());
                                    customer.setCustomerName(bo.getWldwName());
                                    list.add(customer);
                                }
                            }
                            customerService.saveOrUpdateBatch(list);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

    @Override
    public String saveSupplierInfo() {
        try {
//            String token = getToken();
//            if (token != null) {
                //调用erp接口获取数据
//                HttpHeaders httpHeaders = new HttpHeaders();
//                httpHeaders.add("Accept", "application/json");
//                httpHeaders.add("Authorization", "Bearer " + token);
                //思源封装好的代码
//                String result = HttpClient.getRequest(url, null, httpHeaders);
//                JSONArray jsonArray = JSONArray.parseArray(result);
                String url = BASE_URL + "/zp/api/ZpViewInfo/Query";
                Map<String, String> map = new HashMap<>();
                map.put("wldwType", "0");

                Map<String, Object> returnMap = HttpClientss.getDateFromERP(url, HttpMethod.POST, map);
                if (ObjectUtil.equals(returnMap.get("code"), 200)) {
                    if (ObjectUtil.isNotNull(returnMap.get("data")) && StringUtils.isNotEmpty(returnMap.get("data").toString())) {
                        JSONArray jsonArray = JSONArray.parseArray(String.valueOf(returnMap.get("data")));
                        List<SuppInfoBO> suppInfoBOList = JSONObject.parseArray(jsonArray.toJSONString(), SuppInfoBO.class);
//                        supplierManageService.clear();
                        List<SupplierManage> insertList = new ArrayList<>();
//                        List<SupplierManage> updateList = new ArrayList<>();
                        if (suppInfoBOList.size() > 0) {
                            for (SuppInfoBO bo : suppInfoBOList) {
                                SupplierManage supplier = supplierManageService.getOneByField("supplier_code", bo.getWldwCode());
                                if (ObjectUtil.isNull(supplier)) {
                                    SupplierManage supplierManage = new SupplierManage();
                                    supplierManage.setSupplierCode(bo.getWldwCode());
                                    supplierManage.setSid(bo.getId());
                                    supplierManage.setSupplierName(bo.getWldwName());
                                    supplierManage.setLinkMan(bo.getLinkMan());
                                    supplierManage.setLinkPhone(bo.getOfficeTel());
                                    supplierManage.setSupplierEmail(bo.getEmail());
                                    insertList.add(supplierManage);
                                }
                            }
                        }

                        supplierManageService.saveBatch(insertList);
                    }
                }
//            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return null;
    }



    public static void main(String[] args) throws Exception {

        //请求token
        MultiValueMap<String, String> stringMultiValueMap = new LinkedMultiValueMap<>();
        stringMultiValueMap.add("client_id", "KingdeeK3_Rggytest_EpichustClient");
        stringMultiValueMap.add("client_secret", "TekE%dAISMCvwk7V");
        stringMultiValueMap.add("grant_type", "client_credentials");
        String tokenUrl = "https://identityserver:44310/connect/token";
        String clientRg = HttpClient.clientRg(tokenUrl, HttpMethod.POST, stringMultiValueMap);
        JSONObject jsonObject = JSONObject.parseObject(clientRg);
        String token = jsonObject.getString("access_token");
        System.out.println("token ==》" + token);

        if(token != null){

            Map<String, Integer> map1 = new HashMap<>();
            map1.put("currentPage",1);
            map1.put("pageSize",100);

            //调用erp接口获取数据
            CloseableHttpClient client = HttpClients.createDefault();
            URIBuilder uriBuilder = new URIBuilder("http://10.18.3.10:30001/api/ICItem/GetRangeICItems");
            HttpPost post = new HttpPost(uriBuilder.build());
            StringEntity myEntity = new StringEntity(JSON.toJSONString(map1), "UTF-8");// 构造请求数据
            post.setHeader("Content-Type", "application/json");
            post.addHeader("Authorization", "Bearer " + token);
            post.setEntity(myEntity);// 设置请求体
            String responseContent = null; // 响应内容
            CloseableHttpResponse response = null;
            try {
                response = client.execute(post);
                HttpEntity entity = response.getEntity();
                if (response.getStatusLine().getStatusCode() == 200) {
                    String content = EntityUtils.toString(entity, "UTF-8");
                    ItemResponseBO itemResponseBO = JSONArray.parseObject(content, ItemResponseBO.class);
                    String it = itemResponseBO.getList();
                    List<ItemErpBO> outList = (List<ItemErpBO>) JSON
                            .parseArray(it, ItemErpBO.class);
//                    System.out.println("11111111111111111");

                }
            } catch (Exception e) {
                e.printStackTrace();
            }  finally {
                try {
                    if (response != null)
                        response.close();

                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (client != null)
                            client.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    @Override
    public String saveAuxProps() {

        try {
            //请求token
            MultiValueMap<String, String> stringMultiValueMap = new LinkedMultiValueMap<>();
            stringMultiValueMap.add("client_id", "KingdeeK3_Rggytest_EpichustClient");
            stringMultiValueMap.add("client_secret", "TekE%dAISMCvwk7V");
            stringMultiValueMap.add("grant_type", "client_credentials");
            String tokenUrl = "https://identityserver:44310/connect/token";
            String clientRg = HttpClient.clientRg(tokenUrl, HttpMethod.POST, stringMultiValueMap);
            JSONObject jsonObject = JSONObject.parseObject(clientRg);
            String token = jsonObject.getString("access_token");
            System.out.println("token ==》" + token);

            if(token != null){

//                for(int i = 1 ; i<=8; i++){
                    Map<String, Integer> map1 = new HashMap<>();
                    map1.put("currentPage",1);
                    map1.put("pageSize",10000);

                    //调用erp接口获取数据
                    CloseableHttpClient client = HttpClients.createDefault();
                    URIBuilder uriBuilder = new URIBuilder("http://10.18.3.10:30001/api/AuxProp/GetRangeAuxProps/3033");
                    HttpPost post = new HttpPost(uriBuilder.build());
                    StringEntity myEntity = new StringEntity(JSON.toJSONString(map1), "UTF-8");// 构造请求数据
                    post.setHeader("Content-Type", "application/json");
                    post.addHeader("Authorization", "Bearer " + token);
                    post.setEntity(myEntity);// 设置请求体
                    String responseContent = null; // 响应内容
                    CloseableHttpResponse response = null;
                    try {
                        response = client.execute(post);
                        HttpEntity entity = response.getEntity();
                        if (response.getStatusLine().getStatusCode() == 200) {
                            String content = EntityUtils.toString(entity, "UTF-8");
                            ItemResponseBO itemResponseBO = JSONArray.parseObject(content, ItemResponseBO.class);
                            String it = itemResponseBO.getList();
                            List<AuxPropBO> auxPropBOS = (List<AuxPropBO>) JSON
                                    .parseArray(it, AuxPropBO.class);
                            if(auxPropBOS.size() != 0){
                                for(AuxPropBO bo : auxPropBOS){
                                    AuxProp auxProp = new AuxProp();
                                    auxProp.setAuxId(bo.getId());
                                    auxProp.setAuxName(bo.getName());
                                    auxProp.setAuxNumber(bo.getNumber());
                                    auxProp.setUsed(bo.getEnabled());
                                    auxProp.setAuxTypeId(3033);
                                    auxPropMapper.insert(auxProp);

                                }

                            }

                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }  finally {
                        try {
                            if (response != null)
                                response.close();

                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                if (client != null)
                                    client.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
//                }


            }

            return null;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    //异步执行
    @Async
    public void saveErpBack(ErpBackRecord erpBackRecord) {
        this.save(erpBackRecord);
    }

    /**
     * 采购收货回传数据
     * @param list 条件
     * @return
     */
    @Override
    public Map<String, Object> queryErp(List<Map<String,Object>> list) {
        Map<String, Object> returnMap = HttpClientss.getDateFromERP3(ErpInConstant.ERP_IN_RETURL_ASN, HttpMethod.POST, list);
        if (returnMap.get("code")!= null && "200".equals(returnMap.get("code").toString())){
            return returnMap;
        }else {
            System.out.println("回传ERP采购入库失败！");
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"回传ERP采购入库失败！");
        }
    }

    /**
     * ASN通知单查询接口
     * @param hashMap
     * @return
     */
    @Override
    public List<ErpTask> queryAsnNoticeOf(Map<String, String> hashMap) {
        List<ErpTask> billList;
        //获取接收到的数据
        Map<String, Object> returnMap = HttpClientss.getDateFromERP(ErpInConstant.ERP_IN_GETURL_ASN, HttpMethod.POST, hashMap);
        if (returnMap.get("code")!= null && "200".equals(returnMap.get("code").toString())){
            if (returnMap.get("data")!= null && StringUtils.isNotEmpty(returnMap.get("data").toString())){
                JSONArray jsonArray = JSONArray.parseArray(String.valueOf(returnMap.get("data")));
                billList = JSONObject.parseArray(jsonArray.toJSONString(), ErpTask.class);
            }else {
                System.out.println("查询ERP单据信息为空！");
                System.out.println(returnMap);
                throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询ERP单据信息为空！");
            }
        }else {
            System.out.println("查询ERP单据信息失败！");
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询ERP单据信息失败！");
        }
        return billList;
    }

    /**
     * 采购退货回传接口
     * @param purchaseReturn 条件
     * @return
     */
    @Override
    public Map<String, Object> purchaseReturn(Map<String, Object> purchaseReturn) {
        Map<String, Object> urlMap = HttpClientss.getDateFromERP2("http://61.160.125.186:5555/zp/api/ZpViewRtn/Create", HttpMethod.POST, purchaseReturn);
        return urlMap;
    }

    /**
     * 采购退货通知单接口
     * @param hashMap 条件
     * @return
     */
    @Override
    public List<ErpOutTask> purchaseReturnNotice(Map<String, Object> hashMap) {
        List<ErpOutTask> billList = new ArrayList<>();
        //获取接收到的数据
        Map<String, Object> returnMap = HttpClientss.getDateFromERP2(ErpOutConstant.ERP_OUT_GETURL_ASNRET, HttpMethod.POST, hashMap);
        if (returnMap.get("code")!= null && "200".equals(returnMap.get("code").toString())){
            if (returnMap.get("data")!= null && StringUtils.isNotEmpty(returnMap.get("data").toString())){
                JSONArray jsonArray = JSONArray.parseArray(String.valueOf(returnMap.get("data")));
                billList = JSONObject.parseArray(jsonArray.toJSONString(), ErpOutTask.class);
            }else {
                System.out.println("查询ERP单据信息为空！");
                throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询ERP单据信息为空！");
            }
        }else {
            System.out.println("查询ERP单据信息失败！");
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询ERP单据信息失败！");
        }
        return billList;
    }


    /**
     * 销售出货回传接口
     * @param hashMap 条件
     * @return
     */
    @Override
    public Map<String, Object> salesShipment(Map<String, Object> hashMap) {
        Map<String, Object> urlMap = HttpClientss.getDateFromERP2("http://61.160.125.186:5555/zp/api/ZpViewShipPlan/Create", HttpMethod.POST, hashMap);
        return urlMap;
    }

    /**
     * 销售出货查询接口
     * @param hashMap 条件
     * @return
     */
    @Override
    public List<ErpOutTask> salesShipmentQuery(Map<String, Object> hashMap) {
        List<ErpOutTask> billList;
        //获取接收到的数据
        Map<String, Object> returnMap = HttpClientss.getDateFromERP2(ErpOutConstant.ERP_OUT_GETURL_SALES, HttpMethod.POST, hashMap);
        if (returnMap.get("code")!= null && "200".equals(returnMap.get("code").toString())){
            if (returnMap.get("data")!= null && StringUtils.isNotEmpty(returnMap.get("data").toString())){
                JSONArray jsonArray = JSONArray.parseArray(String.valueOf(returnMap.get("data")));
                billList = JSONObject.parseArray(jsonArray.toJSONString(), ErpOutTask.class);
            }else {
                System.out.println("查询ERP单据信息为空！");
                throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询ERP单据信息为空！");
            }
        }else {
            System.out.println("查询ERP单据信息失败！");
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询ERP单据信息失败！");
        }
        return billList;
    }

    /**
     * 销售退货回传接口
     * @param hashMap 条件
     * @return
     */
    @Override
    public Map<String, Object> salesReturn(Map<String, Object> hashMap) {
        Map<String, Object> urlMap = HttpClientss.getDateFromERP2("http://61.160.125.186:5555/zp/api/ZpViewRtnRcv/Create", HttpMethod.POST, hashMap);
        return urlMap;
    }

    /**
     * 销售退货申请单接口
     * @param hashMap 条件
     * @return
     */
    @Override
    public List<ErpTask>  salesReturnApplication(Map<String, Object> hashMap) {
        List<ErpTask> billList;
        //获取接收到的数据
        Map<String, Object> returnMap = HttpClientss.getDateFromERP2(ErpInConstant.ERP_IN_GETURL_SALESRETURN, HttpMethod.POST, hashMap);
        if (returnMap.get("code")!= null && "200".equals(returnMap.get("code").toString())){
            Object data = returnMap.get("data");
            if (null != data){
                JSONArray jsonArray = JSONArray.parseArray(String.valueOf(returnMap.get("data")));
                billList = JSONObject.parseArray(jsonArray.toJSONString(), ErpTask.class);
            }else {
                System.out.println("查询ERP单据信息为空！");
                throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询ERP单据信息为空！");
            }
        }else {
            System.out.println("查询ERP单据信息失败！");
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询ERP单据信息失败！");
        }
        return billList;
    }

    /**
     * 生产领料回传接口
     * @param hashMap 条件
     * @return
     */
    @Override
    public Map<String, Object> productionOfPicking(Map<String, Object> hashMap) {
        Map<String, Object> urlMap = HttpClientss.getDateFromERP2("http://61.160.125.186:5555/zp/api/ZpViewPick/Create", HttpMethod.POST, hashMap);
        return urlMap;
    }

    /**
     * 生产领料申请单接口
     * @param hashMap 条件
     * @return
     */
    @Override
    public List<ErpOutTask> productionOfPickingApplication(Map<String, Object> hashMap) {
        List<ErpOutTask> billList;
        //获取接收到的数据
        Map<String, Object> returnMap = HttpClientss.getDateFromERP2(ErpOutConstant.ERP_OUT_GETURL_ZF, HttpMethod.POST, hashMap);
        if (ObjectUtil.equal(200, returnMap.get("code"))){
            if (ObjectUtil.isNotNull(returnMap.get("data"))){
                JSONArray jsonArray = JSONArray.parseArray(String.valueOf(returnMap.get("data")));
                billList = JSONObject.parseArray(jsonArray.toJSONString(), ErpOutTask.class);
            }else {
                System.out.println("查询ERP单据信息为空！");
                throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询ERP单据信息为空！");
            }
        }else {
            System.out.println("查询ERP单据信息失败！");
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询ERP单据信息失败！");
        }
        return billList;
    }

    /**
     * 生产退料回传接口
     * @param hashMap 条件
     * @return
     */
    @Override
    public Map<String, Object> productionMaterialReturn(Map<String, Object> hashMap) {
        Map<String, Object> urlMap = HttpClientss.getDateFromERP2("http://61.160.125.186:5555/zp/api/ZpViewRtnPick/Create", HttpMethod.POST, hashMap);
        return urlMap;
    }

    /**
     * 生产退料申请单接口
     * @param hashMap 条件
     * @return
     */
    @Override
    public List<ErpTask> productionMaterialReturnApplication(Map<String, Object> hashMap) {
        List<ErpTask> billList = new ArrayList<>();
        //获取接收到的数据
        Map<String, Object> returnMap = HttpClientss.getDateFromERP2(ErpInConstant.ERP_IN_GETURL_PRDRETURN, HttpMethod.POST, hashMap);
        if (returnMap.get("code")!= null && "200".equals(returnMap.get("code").toString())){
            if (returnMap.get("data")!= null && StringUtils.isNotEmpty(returnMap.get("data").toString())){
                JSONArray jsonArray = JSONArray.parseArray(String.valueOf(returnMap.get("data")));
                billList = JSONObject.parseArray(jsonArray.toJSONString(), ErpTask.class);
            }else {
                System.out.println("查询ERP单据信息为空！");
                throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询ERP单据信息为空！");
            }
        }else {
            System.out.println("查询ERP单据信息失败！");
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询ERP单据信息失败！");
        }
        return billList;
    }

    /**
     * 完工报告回传接口
     * @param hashMap 条件
     * @return
     */
    @Override
    public Map<String, Object> completionOfTheReport(Map<String, Object> hashMap) {
        Map<String, Object> urlMap = HttpClientss.getDateFromERP2("http://61.160.125.186:5555/zp/api/ZpViewConplete/Create", HttpMethod.POST, hashMap);
        return urlMap;
    }

    /**
     * 完工报告生产订单接口
     * @param hashMap 条件
     * @return
     */
    @Override
    public List<ErpTask> completionOfTheReportApplication(Map<String, Object> hashMap) {
        List<ErpTask> billList;
        //获取接收到的数据
        Map<String, Object> returnMap = HttpClientss.getDateFromERP2(ErpInConstant.ERP_IN_GETURL_PRD, HttpMethod.POST, hashMap);
        if (returnMap.get("code")!= null && "200".equals(returnMap.get("code").toString())){
            if (returnMap.get("data")!= null && StringUtils.isNotEmpty(returnMap.get("data").toString())){
                JSONArray jsonArray = JSONArray.parseArray(String.valueOf(returnMap.get("data")));
                billList = JSONObject.parseArray(jsonArray.toJSONString(), ErpTask.class);
            }else {
                System.out.println("查询ERP单据信息为空！");
                throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询ERP单据信息为空！");
            }
        }else {
            System.out.println("查询ERP单据信息失败！");
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询ERP单据信息失败！");
        }
        return billList;
    }

    /**
     * 调拨出库*委外发科回传接口
     * @param hashMap 条件
     * @return
     */
    @Override
    public Map<String, Object> inAllocatingOutbound(Map<String, Object> hashMap) {
        Map<String, Object> urlMap = HttpClientss.getDateFromERP2("http://61.160.125.186:5555/zp/api/ZpViewTransOut/Create", HttpMethod.POST, hashMap);
        return urlMap;
    }

    /**
     * 调拨出库*委外发科申请单接口
     * @param hashMap 条件
     * @return
     */
    @Override
    public List<ErpOutTask> inAllocatingOutboundApplication(Map<String, Object> hashMap) {
        List<ErpOutTask> billList = null;
        //获取接收到的数据
//        Map<String, Object> returnMap = HttpClientss.getDateFromERP2(ErpOutConstant.ERP_OUT_GETURL_OUTSOURCE, HttpMethod.POST, hashMap);
//        if (ObjectUtil.equal(returnMap.get("code"), 200)){
//            if (ObjectUtil.isNotNull(returnMap.get("data"))){
//                JSONArray jsonArray = JSONArray.parseArray(String.valueOf(returnMap.get("data")));
//                billList = JSONObject.parseArray(jsonArray.toJSONString(), ErpOutTask.class);
//            }else {
//                System.out.println("查询ERP单据信息为空！");
//                throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询ERP单据信息为空！");
//            }
//        }else {
//            System.out.println("查询ERP单据信息失败！");
//            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询ERP单据信息失败！");
//        }
        return billList;
    }

    /**
     * 杂收单回传接口
     * @param hashMap 条件
     * @return
     */
    @Override
    public Map<String, Object> miscellaneousChargeSheet(Map<String, Object> hashMap) {
        Map<String, Object> urlMap = HttpClientss.getDateFromERP2("http://61.160.125.186:5555/zp/api/ZpViewMisc/Create", HttpMethod.POST, hashMap);
        return urlMap;
    }

    /**
     * 杂发单回传接口
     * @param hashMap 条件
     * @return
     */
    @Override
    public Map<String, Object> miscellaneous(Map<String, Object> hashMap) {
        Map<String, Object> urlMap = HttpClientss.getDateFromERP2("http://61.160.125.186:5555/zp/api/ZpViewMiscShip/Create", HttpMethod.POST, hashMap);
        return urlMap;
    }

    /**
     * 日常盘点回传单
     * @param hashMap 条件
     * @return
     */
    @Override
    public Map<String, Object> dailyInventoryListReturn(Map<String, Object> hashMap) {
        Map<String, Object> urlMap = HttpClientss.getDateFromERP2("http://61.160.125.186:5555/zp/api/ZpViewInv/Create", HttpMethod.POST, hashMap);
        return urlMap;
    }

    /**
     * 日常盘点查询单
     * @param hashMap 条件
     * @return
     */
    @Override
    public List<ReturnGoods> dailyInventoryList(Map<String, Object> hashMap) {
        List<ReturnGoods> billList = new ArrayList<>();
        //获取接收到的数据
        Map<String, Object> returnMap = HttpClientss.getDateFromERP2("http://61.160.125.186:5555/zp/api/ZpViewInv/Query", HttpMethod.POST, hashMap);
        if (returnMap.get("code")!= null && "200".equals(returnMap.get("code").toString())){
            if (returnMap.get("data")!= null && StringUtils.isNotEmpty(returnMap.get("data").toString())){
                JSONArray jsonArray = JSONArray.parseArray(String.valueOf(returnMap.get("data")));
                billList = JSONObject.parseArray(jsonArray.toJSONString(), ReturnGoods.class);
            }else {
                System.out.println("查询ERP单据信息为空！");
                throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询ERP单据信息为空！");
            }
        }else {
            System.out.println("查询ERP单据信息失败！");
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询ERP单据信息失败！");
        }
        return billList;
    }

    /**
     * 物料(料品)查询 回传
     * @param hashMap 条件
     * @return
     */
    @Override
    public List<ItemInfo> itemInfoList(Map<String, Object> hashMap) {
        List<ItemInfoVo> billList = new ArrayList<>();
        List<ItemInfo> list = new ArrayList<>();
        Map<String, Object> returnMap = HttpClientss.getDateFromERP2("http://61.160.125.186:5555/zp/api/ZpViewItemMaster/Query", HttpMethod.POST, hashMap);
        if (returnMap.get("code")!= null && "200".equals(returnMap.get("code").toString())){
            if (returnMap.get("data")!= null && StringUtils.isNotEmpty(returnMap.get("data").toString())){
                JSONArray jsonArray = JSONArray.parseArray(String.valueOf(returnMap.get("data")));
                billList = JSONObject.parseArray(jsonArray.toJSONString(), ItemInfoVo.class);
                for (ItemInfoVo vo:billList){
                    ItemInfo itemInfo = new ItemInfo();
                    itemInfo.setItemCode(vo.getItemCode());
                    itemInfo.setItemName(vo.getItemName());
                    itemInfo.setItemType(vo.getItemType());
                    itemInfo.setStatus(vo.getStatus());
                    itemInfo.setSpec(vo.getSpec());
                    itemInfo.setOrg(vo.getOrg());
                    itemInfo.setMakeDate(vo.getMakeDate());
                    itemInfo.setEditDate(vo.getEditDate());
                    list.add(itemInfo);
                }
            }else {
                System.out.println("查询ERP单据信息为空！");
                throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询ERP单据信息为空！");
            }
        }else {
            System.out.println("查询ERP单据信息失败！");
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询ERP单据信息失败！");
        }
        return list;
    }

    /**
     * 往来单位查询 回传
     * @param hashMap 条件
     * @return
     */
    @Override
    public Map<String, Object> withTheUnit(Map<String, Object> hashMap) {
        Map<String, Object> urlMap = HttpClientss.getDateFromERP2("http://61.160.125.186:5555/zp/api/ZpViewInfo/Query", HttpMethod.POST, hashMap);
        return urlMap;
    }

    /**
     * 委外退料查询接口
     * @param hashMap
     * @return
     */
    @Override
    public List<ErpTask> getOutSourceBillNo(Map<String, Object> hashMap) {
        List<ErpTask> billList;
        Map<String, Object> returnMap = HttpClientss.getDateFromERP2(ErpInConstant.ERP_IN_GETURL_OSRETURN, HttpMethod.POST, hashMap);
        if (returnMap.get("code")!= null && "200".equals(returnMap.get("code").toString())){
            if (returnMap.get("data")!= null && StringUtils.isNotEmpty(returnMap.get("data").toString())){
                JSONArray jsonArray = JSONArray.parseArray(String.valueOf(returnMap.get("data")));
                billList = JSONObject.parseArray(jsonArray.toJSONString(), ErpTask.class);
            }else {
                System.out.println("查询ERP委外退料单据信息为空！");
                throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询ERP委外退料单据信息为空！");
            }
        }else {
            System.out.println("查询委外退料单失败！");
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询委外退料单失败！");
        }
        return billList;
    }

    /**
     * 委外收货查询接口
     * @param hashMap
     * @return
     */
    @Override
    public List<ErpTask> queryOtherIn(Map<String, Object> hashMap) {
        List<ErpTask> billList;
        //获取接收到的数据
        Map<String, Object> returnMap = HttpClientss.getDateFromERP2(ErpInConstant.ERP_IN_GETURL_OUTSOURCE, HttpMethod.POST, hashMap);
        if (returnMap.get("code")!= null && "200".equals(returnMap.get("code").toString())){
            if (returnMap.get("data")!= null && StringUtils.isNotEmpty(returnMap.get("data").toString())){
                JSONArray jsonArray = JSONArray.parseArray(String.valueOf(returnMap.get("data")));
                billList = JSONObject.parseArray(jsonArray.toJSONString(), ErpTask.class);
            }else {
                System.out.println("查询ERP委外收货为空！");
                System.out.println(returnMap);
                throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询ERP委外收货信息为空！");
            }
        }else {
            System.out.println("查询ERP委外收货信息失败！");
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"查询ERP委外收货信息失败！");
        }
        return billList;
    }

}
